package cn.lg.soar.general.client;

import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.general.api.constant.ModuleConst;
import cn.lg.soar.general.api.model.BizFileDTO;
import cn.lg.soar.general.api.model.FileDigestVO;
import cn.lg.soar.general.api.model.FileVO;
import cn.lg.soar.general.api.service.IFileApi;
import cn.lg.soar.general.entity.FileMetadata;
import cn.lg.soar.general.service.FileAccessService;
import cn.lg.soar.general.service.FileUploadService;
import cn.lg.soar.general.service.IBizFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author luguoxiang
 * @date 2022/1/16
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiIgnore
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/file")
public class FileApiClient implements IFileApi {

    @Autowired
    private FileUploadService uploadService;
    @Autowired
    private FileAccessService accessService;
    @Autowired
    private IBizFileService bizFileRelationService;

    @PostMapping("fastUpload-biz")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<FileVO> fastUpload(Long bizId, String bizType, @RequestBody Collection<FileDigestVO> vos) {
        List<FileMetadata> metadataList = uploadService.fastUpload(vos);
        List<FileVO> fileVOS = uploadService.toVos(metadataList);
        bizFileRelationService.saveBizFile(bizId, BizFileDTO.builder().add(bizType, fileVOS).build());
        return fileVOS;
    }

    @PostMapping("fastUpload")
    @Override
    public List<FileVO> fastUpload(@RequestBody Collection<FileDigestVO> vos) {
        List<FileMetadata> metadataList = uploadService.fastUpload(vos);
        return uploadService.toVos(metadataList);
    }

    @PostMapping("upload-biz")
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<FileVO> upload(Long bizId, String bizType, Collection<MultipartFile> files) {
        List<FileMetadata> metadataList = uploadService.upload(files);
        List<FileVO> fileVOS = uploadService.toVos(metadataList);
        bizFileRelationService.saveBizFile(bizId, BizFileDTO.builder().add(bizType, fileVOS).build());
        return fileVOS;
    }

    @PostMapping("upload-bucket")
    @Override
    public List<FileVO> upload(String bucketName, Collection<MultipartFile> files) {
        List<FileMetadata> metadataList = uploadService.upload(bucketName, files);
        return uploadService.toVos(metadataList);
    }

    @PostMapping("upload")
    @Override
    public List<FileVO> upload(Collection<MultipartFile> files) {
        List<FileMetadata> metadataList = uploadService.upload(files);
        return uploadService.toVos(metadataList);
    }

    @PostMapping("saveBizFile")
    @Override
    public boolean saveBizFile(Long bizId, @RequestBody Collection<BizFileDTO> bizFiles) {
        return bizFileRelationService.saveBizFile(bizId, bizFiles);
    }

    @GetMapping("getBizFile")
    @Override
    public Map<String, List<FileVO>> getBizFile(Long bizId, Collection<String> bizTypes) {
        return bizFileRelationService.getBizFile(bizId, bizTypes);
    }

    @PostMapping("removeBizFile")
    @Override
    public boolean removeBizFile(Long bizId, Collection<String> bizTypes) {
        return bizFileRelationService.removeBizFile(bizId, bizTypes);
    }

    @GetMapping("createTemporaryUrl")
    @Override
    public String createTemporaryUrl(boolean anonymous, long fileId, long expire, Set<Long> userIds) {
        return accessService.createTemporaryUrl(anonymous, fileId, expire, userIds);
    }

    @GetMapping("createFixedSecretUrl")
    @Override
    public String createFixedSecretUrl(long fileId) {
        return accessService.createFixedSecretUrl(fileId);
    }

}
